package cn.xiaohuodui.util;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageUtil {

    public static List<Integer> pagePrepare(HttpServletRequest request) {
        String limitStr = request.getParameter("length");

        int limit = limitStr == null ? 10 : Integer.parseInt(limitStr);

        String offsetStr = request.getParameter("start");
        int offset = offsetStr == null ? 0 : Integer.parseInt(offsetStr);

        String drawStr = request.getParameter("draw");
        int draw = drawStr == null ? 0 : Integer.parseInt(drawStr);


        List<Integer> list = new ArrayList<>();
        list.add(limit);
        list.add(offset);
        list.add(draw);
        return list;
    }

    /**
     * 返回一个 固定的 json 模板
     * 模板 是根据 dataTable 返回的数据类型来的
     *
     * @param draw
     * @param datas
     * @param count
     * @return
     */
    public static Map<String, Object> pageResult(int draw, Object datas, int count) {
        Map<String, Object> results = new HashMap<>();
        results.put("draw", draw);
        results.put("data", datas);
        results.put("recordsFiltered", count);
        results.put("recordsTotal", count);

        return results;
    }

}
